package com.yy.bootdemo.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.dto.MenuIndexDto;
import com.yy.bootdemo.admin.entity.MyUser;
import com.yy.bootdemo.admin.service.UserService;
import com.yy.bootdemo.common.utils.PageTableRequest;
import com.yy.bootdemo.common.utils.Result;
import com.yy.bootdemo.common.utils.UserConstants;
import com.yy.bootdemo.log.aop.MyLog;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author codermy
 * @createTime 2020/7/10
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;



    @PostMapping("/list1")

    @MyLog("查询用户")
    public Result<MyUser> userList(@RequestBody PageTableRequest pageTableRequest,@RequestBody MyUser myUser){

        return null;
    }
    @GetMapping("/getUserByid")
    public  MyUser getUserByid(@RequestParam Integer userId) {
        return userService.getUserById(userId);
    }


    
    public  MyUser getByid(@RequestParam Integer userId) {
        return userService.getUserById(userId);
    }

    @PostMapping(value = "/list")
    @PreAuthorize("hasAnyAuthority('user:list')")

    public List<MenuIndexDto> list() {

        return userService.getUserlist();
    }


    @RequestMapping("/pagelist")
    public R pagelist(@RequestBody PageUtils params){
        // 条件构造器
        QueryWrapper<MyUser> userQuery = Wrappers.query();


        Page<MyUser> page = new Page<>(params.getCurrent(), params.getSize());

        IPage<MyUser> userPage = userService.page(page, userQuery);

        List<MyUser> userList = userPage.getRecords();
        return R.ok(userPage);
    }
    @GetMapping("getUserList")
    public R getUserList(){
        // 条件构造器
        QueryWrapper<MyUser> userQuery = Wrappers.query();

        userQuery.in("type",0,1);
        List<MyUser> list=  userService.list(userQuery);
        return R.ok(list);
    }


    @GetMapping("getById")
    public R getUser(@RequestParam Integer userId){
        return R.ok(userService.getById(userId));
    }

    @PostMapping("save1")
    public R save1(@RequestBody MyUser myMenu){
        userService.save(myMenu);
        return R.ok();
    }

    @PostMapping("updateById")
    @PreAuthorize("hasAnyAuthority('user:edit')")
    public R updateById(@RequestBody MyUser myMenu){
        userService.updateById(myMenu);
        return R.ok();
    }










    @PostMapping("save")

    @PreAuthorize("hasAnyAuthority('user:add')")
    @MyLog("添加用户")
    public Result<MyUser> saveUser(@RequestBody MyUser myUser){
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(myUser))){
            return Result.error().message("手机号已存在");
        }
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkUserNameUnique(myUser))){
            return Result.error().message("用户名已存在");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        myUser.setPassword(bCryptPasswordEncoder.encode("123456"));
        return userService.save(myUser,myUser.getRoleId());
    }


}
